﻿// --------------------------------------------------------------------------------------
// <copyright file="noi_dungController.cs" company="NGroup">
// Copyright (c) NGroup. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------
// <history>
//     <change who="Bui Thanh Nam" date="2014.01.05">Create</change>
// </history>
// --------------------------------------------------------------------------------------

namespace NGroup.VTL.Web.Controllers
{
    using System.Web.Mvc;
    using NGroup.VTL.Service.Abstraction;
    using NGroup.VTL.Service;
    using NGroup.VTL.Models;
    using NGroup.VTL.WebCore;
    using NGroup.VTL.Core.Const;
    using System.Collections.Generic;

    public class noi_dungController : ContentBaseController
    {
        #region Constructor

        private IRetrieveContentService _retrieveContentService;

        public noi_dungController()
        {
            _retrieveContentService = new RetrieveContentService();
        }

        public noi_dungController(IRetrieveContentService retrieveContentService)
        {
            _retrieveContentService = retrieveContentService;
        }

        #endregion

        public ActionResult loai_san_pham_L0(string parentUniqueString)
        {
            PagingInfoModel pagingInfo = this.GetPagingParameters();
            var contentTypeItem = AppContentTypes.All.Find(e => !e.SupTypeID.HasValue && e.UniqueString == parentUniqueString);

            int parentTypeID = contentTypeItem.TypeID.Value;
            int? childTypeID = null;

            var productModelList = _retrieveContentService.GetProductListByTypeAndPagingInfo(parentTypeID, childTypeID, CurrentLanguage, ref pagingInfo);

            string pageTitle, metaKeyword, metaDescription;

            if (CurrentLanguage == LanguageCodes.en)
            {
                pageTitle = contentTypeItem.Description_En;
                metaKeyword = contentTypeItem.MetaKeyword_En;
                metaDescription = contentTypeItem.MetaDescription_En;
            }
            else
            {
                pageTitle = contentTypeItem.Description;
                metaKeyword = contentTypeItem.MetaKeyword;
                metaDescription = contentTypeItem.MetaDescription;
            }    

            ViewBag.PagingInfo = pagingInfo;

            ViewBag.Title = pageTitle;
            ViewBag.ParentUniqueString = parentUniqueString;
            ViewBag.MetaDescription = metaDescription;
            ViewBag.MetaKeyword = metaKeyword;

            return View(productModelList);
        }

        public ActionResult loai_san_pham_L1(string parentUniqueString, string childUniqueString)
        {
            PagingInfoModel pagingInfo = this.GetPagingParameters();
            var parentContentTypeItem = AppContentTypes.All.Find(e => !e.SupTypeID.HasValue && e.UniqueString == parentUniqueString);
            int parentTypeID = parentContentTypeItem.TypeID.Value;

            var childContentTypeItem = AppContentTypes.All.Find(e => e.SupTypeID.HasValue && e.SupTypeID.Value == parentTypeID && e.UniqueString == childUniqueString);
            int? childTypeID = childContentTypeItem.TypeID;

            var productModelList = _retrieveContentService.GetProductListByTypeAndPagingInfo(parentTypeID, childTypeID, CurrentLanguage, ref pagingInfo);
            ViewBag.PagingInfo = pagingInfo;
            string parentDescription, childDescription, metaKeyword, metaDescription;

            if (CurrentLanguage == LanguageCodes.en)
            {
                parentDescription = parentContentTypeItem.Description_En;
                childDescription = childContentTypeItem.Description_En;
                metaKeyword = childContentTypeItem.MetaKeyword_En;
                metaDescription = childContentTypeItem.MetaDescription_En;
            }
            else
            {
                parentDescription = parentContentTypeItem.Description;
                childDescription = childContentTypeItem.Description;
                metaKeyword = childContentTypeItem.MetaKeyword;
                metaDescription = childContentTypeItem.MetaDescription;
            }            

            ViewBag.Title = string.Join(" - ", parentDescription, childDescription);
            ViewBag.PagingFormAction = string.Join("/", new string[] { parentUniqueString, childUniqueString });
            ViewBag.MetaDescription = metaDescription;
            ViewBag.MetaKeyword = metaKeyword;

            return View(productModelList);
        }

        public ActionResult chi_tiet_san_pham(string parentUniqueString, string childUniqueString, string productUniqueString)
        {
            var parentContentTypeItem = AppContentTypes.All.Find(e => !e.SupTypeID.HasValue && e.UniqueString == parentUniqueString);
            int parentTypeID = parentContentTypeItem.TypeID.Value;

            var childContentTypeItem = AppContentTypes.All.Find(e => e.SupTypeID.HasValue && e.SupTypeID.Value == parentTypeID && e.UniqueString == childUniqueString);
            int childTypeID = childContentTypeItem.TypeID.Value;

            List<ProductModel> relatedProducts = null;

            var productModel = _retrieveContentService.GetProductDetail(parentTypeID, childTypeID, productUniqueString, CurrentLanguage, out relatedProducts);

            ViewBag.Title = productModel.Title;
            ViewBag.MetaKeyword = productModel.MetaContent;
            ViewBag.MetaDescription = productModel.MetaDescription;
            ViewBag.RelatedProducts = relatedProducts;

            return View(productModel);
        }

        public ActionResult dat_mua_san_pham(OrderModel order)
        {
            return View();
        }

        private PagingInfoModel GetPagingParameters()
        {
            var pagingInfo = new PagingInfoModel();

            string currentPage = Request.Form["currentPage"];
            string pageSize = Request.Form["pageSize"];

            int currentPageValue, pageSizeValue;

            pagingInfo.CurrentPage = int.TryParse(currentPage, out currentPageValue) ? currentPageValue : 1;
            pagingInfo.PageSize = int.TryParse(pageSize, out pageSizeValue) ? pageSizeValue : GeneralConfiguration.PageSize;

            return pagingInfo;
        }
    }
}
